﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Pigs.Enemies
{
    class Drunk_Pig : pigs.Pig
    {
        private float vert;
        private bool up;

        public Texture2D drunkPigTexture;

        // Animation variables
        int frameCount = 0; // Current frame
        int frameSkipX = 124; // Width of a frame
        int animationCount; // How many ticks since the last frame change.
        int animationMax; // How many ticks to change frame after. 
        Rectangle source;

        /// <summary>
        /// make the new pig
        /// </summary>
        /// <param name="texture">texture to use</param>
        /// <param name="position">where should the pig start out</param>
        public Drunk_Pig(Texture2D texture, Vector2 position, int numPlayers)
            : base(texture, position)
        {
            START_HEALTH = 5;
            START_DAMAGE = 10;
            START_SPEED = 3.0f;
            VALUE = 125;

            this.position = position;
            this.drunkPigTexture = texture;
            this.active = true; // We initialize the enemy to be active so it will be update in the game
            if (numPlayers == 1) { this.health = START_HEALTH; }
            else if (numPlayers > 1 && numPlayers < 3) { this.health = START_HEALTH * 2; }
            else { this.health = this.START_HEALTH * 3; }
            if (numPlayers == 1) { this.damage = this.START_DAMAGE; }
            else if (numPlayers > 2 && numPlayers < 5) { this.damage = this.START_DAMAGE + 5; }
            else { this.damage = this.START_DAMAGE + 10; }
            this.speed = START_SPEED;
            Random rand = new Random();
            this.vert = rand.Next(5);
            this.value = VALUE;
            this.effect = SpriteEffects.None;
            this.ID = "drunk";
            this.up = false;
            this.lead = false;
            this.buff = false;
            this.escaped = false;
            this.hitByFlame = false;
            this.ghost = false;

            // Start with frame 1 of image array
            this.frameCount = 1;
            this.animationCount = 0;
            this.animationMax = 3;

            this.source = new Rectangle(this.frameSkipX * this.frameCount, 0, this.frameSkipX, this.texture.Height);

            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);

            //get the color data
            colorMap = new Color[source.Width * source.Height];
            this.texture.GetData(0, source, colorMap, 0, source.Width * source.Height);
        }


        public override void doAI()
        {
            // If a drunk pig gets the leader buff, drastically increas its damage and health
            if (this.lead && !this.buff)
            {
                this.health *= 3;
                this.damage *= 2;
                this.buff = true;
            }
            // Drunk pig's AI is to move in a wave
            if (up)
            {
                if (this.vert < 5)
                {
                    this.vert += 0.2f;
                }
                else
                {
                    this.up = false;
                }
            }
            else
            {
                if (this.vert > -5)
                {
                    this.vert -= 0.2f;
                }
                else
                {
                    this.up = true;
                }
            }
            //Move the pig
            this.position.X -= this.speed;
            this.position.Y += this.vert;
            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);
            if (health <= 0)
            {
                // By setting the Active flag to false, the game will remove this object from the 
                // active game list
                active = false;
            }
        }

        public override void drunkUpdate(GameTime gameTime)
        {
            if (this.shouldGhost && this.color.A > 70)
            {
                this.color.A--;
                this.color.A--;
                this.color.A--;
            }

            this.doAI();
            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);
            UpdateAnimation();
        }

        // Perform the animation updates on the image
        public void UpdateAnimation()
        {
            this.animationCount += 1;
            if (this.animationCount > this.animationMax)
            {
                this.animationCount = 0;
                this.frameCount += 1;
                if (this.frameCount == 5)
                {
                    this.frameCount = 0;
                }
            }
        }

        //Draw the updated texture image
        public override void Draw(SpriteBatch spriteBatch)
        {
            // Update the source rectangle, based on where in the animation we are.  
            this.source.X = this.frameSkipX * this.frameCount;

            spriteBatch.Draw(texture, position, this.source, color, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
        }
    }
}
